/**
 * Created by admin on 2017/4/8.
 */

window.onload=function () {
    var http = "http://new.yytaomeng.com";
    /*--------------------贷款产品数据json-------------------------*/
    var data = [
        {
            "name": "玖富叮当贷-极速贷",
            "start": 5,//5
            "url": "https://cube.doraemoney.com/newCube/index.vue?proId=84f04d403a3144baa70e524043f4f79e",
            "logo": "/upload/image/20161025/1477378663786077678.png",
            "rate": "0.98%-1.2%/月",
            "term_range": "3-48",
            "loans_scope": "1000-100000",
            "loan_time": "当天",
            "loans_type": 1,
            "plat_order": 1
        }, {
            "name": "贷你嗨-代还信用卡",
            "start": 5,
            "url": "https://lhwap.dainihai.cn/share.html?type=jiekuanwang&from=singlemessage",
            "logo": "/upload/image/20170325/1490410738134042673.png",
            "rate": "0.05%/日",
            "term_range": "3-24",//3-24
            "loans_scope": "1000-50000",
            "loan_time": "当天",
            "loans_type": 1,
            "plat_order": 2
        }, {
            "name": "快易花",
            "start": 5,
            "url": "http://t.cn/RJs7SFF",
            "logo": "/upload/image/20170213/1486956373700075707.jpg",
            "rate": "0.5%-0.76%/月",
            "term_range": "1-24",
            "loans_scope": "1000-50000",
            "loan_time": "1天",
            "loans_type": 1,
            "plat_order": 3
        }, {
            "name": "手机贷",
            "start": 5,
            "url": "http://sjd-m.mobanker.com/?channel=zqxq-llcs",
            "logo": "/upload/image/20161211/1481428032559024696.png",
            "rate": "0.03%/日",
            "term_range": "1-12",
            "loans_scope": "1000-10000",
            "loan_time": "当天",
            "loans_type": 1,
            "plat_order": 4
        }, {
            "name": " 你我贷-嘉卡贷",
            "start": 5,
            "url": "http://m.niwodai.com/index.do?method=ac&artId=3800000713376124&nwd_ext_aid=3000001700333155&source_id=",
            "logo": "/upload/image/20161009/1476006530207082905.png",
            "rate": "0.5%/月",
            "term_range": "6-48",
            "loans_scope": "3000-50000",
            "loan_time": "2小时",
            "loans_type": 1,
            "plat_order": 5
        }, {
            "name": "小赢卡贷-代还信用卡",
            "start": 5,
            "url": "https://cardloan.xiaoying.com/kadaicredit/index?source=10000249",
            "logo": "/upload/image/20170113/1484300192289094411.png",
            "rate": "0.34%/月",
            "term_range": "1-12",
            "loans_scope": "2000-50000",
            "loan_time": "最快3分钟",
            "loans_type": 1,
            "plat_order": 6
        }, {
            "name": "平安普惠-大额救急贷",
            "start": 5,
            "url": "https://www.10100000.com/m/iloan/apply2.html?utm_source=phmd-mdcqzqxqhk&utm_medium=mkt&utm_campaign=mkt&utm_content=n01&WT.mc_id=CXX-MDCQZQXQHK-n01-&",
            "logo": "/upload/image/20170320/1489998450086058678.png",
            "rate": "0.98%/月",
            "term_range": "1-36",
            "loans_scope": "2000-30000",
            "loan_time": "3分钟",
            "loans_type": 1,
            "plat_order": 7
        }, {
            "name": "先花一亿元",
            "start": 5,
            "url": "http://weixin.xianhuahua.com/dev/coupon/couponappsix?from=1028",
            "logo": "/upload/image/20160910/1473465265410099979.png",
            "rate": "0.05%/天",
            "term_range": "1-3",
            "loans_scope": "500-10000",
            "loan_time": "10分钟",
            "loans_type": 1,
            "plat_order": 8
        }, {
            "name": "卡卡贷-代还信用卡",
            "start": 5,
            "url": "http://game.kkcredit.cn/download/renxingdai/kakaadv1",
            "logo": "/upload/image/20160719/1468895303680044861.png",
            "rate": "0.9%-1.6%/月",
            "term_range": "6-24",
            "loans_scope": "1000-50000",
            "loan_time": "当天",
            "loans_type": 1,
            "plat_order": 9
        }, {
            "name": "简单借款",
            "start": 5,
            "url": "https://activity.jiandanjiekuan.com/html/register_getNewUser.html?channel=act_act5_jiekuanwang",
            "logo": "/upload/image/20170113/1484297975700053002.png",
            "rate": "0.03%/日",
            "term_range": "1-3",
            "loans_scope": "100-5000",
            "loan_time": "当天",
            "loans_type": 1,
            "plat_order": 10
        }, {
            "name": "魔法现金",
            "start": 5,
            "url": "http://aldb.me/Zw1lR",
            "logo": "/upload/image/20161103/1478145002814071249.png",
            "rate": "1.0%/月",
            "term_range": "1-3",
            "loans_scope": "1-5000",
            "loan_time": "3分钟放款",
            "loans_type": 1,
            "plat_order": 11
        }, {
            "name": "身份贷-大额贷",
            "start": 5,
            "url": "https://m.rong360.com/express?from=sem21&utm_source=rxd&utm_medium=dk&utm_campaign=cs1",
            "logo": "/upload/image/20160719/1468894105407011499.jpg",
            "rate": "1%/月",
            "term_range": "1-60",
            "loans_scope": "1000-500000",
            "loan_time": "当天",
            "loans_type": 1,
            "plat_order": 12
        }, {
            "name": "向钱贷",
            "start": 5,
            "url": "https://m.xiangqd.cn/login?channel=jkw",
            "logo": "/upload/image/20170324/1490326349743058005.png",
            "rate": "1%-2%/月",
            "term_range": "0-1",
            "loans_scope": "300-1200",
            "loan_time": "15分钟",
            "loans_type": 1,
            "plat_order": 13
        }, {
            "name": "奇速贷-小额极速",
            "start": 5,
            "url": "http://a2681.oadz.com/link/C/2681/31/2rRxmUzxN0Yy6B6Dl5cN5t82rvk_/p049/0/https://qsd.qizidai.com/static/qsd_prop/invite_reg.html?",
            "logo": "/upload/image/20170310/1489141167317075994.png",
            "rate": "1.0%／月",
            "term_range": "0-1",
            "loans_scope": "1000-3000",
            "loan_time": "20分钟",
            "loans_type": 1,
            "plat_order": 14
        }, {
            "name": "钱有路",
            "start": 5,
            "url": "http://static.namifunds.com/qyl/web/views/invitation.html?iv_code=FEF226013&channel=yy-6",
            "logo": "/upload/image/20170328/1490685654949062829.png",
            "rate": "1%/月",
            "term_range": "0-1",
            "loans_scope": "500-1000",
            "loan_time": "1分钟",
            "loans_type": 1,
            "plat_order": 15
        }, {
            "name": "速贷-信用卡公积金贷款",
            "start": 5,
            "url": "http://tg.mifengkong.cn/sudai/20161213_01/build/index.vue?cid=0010",
            "logo": "/upload/image/20170113/1484309487652054609.jpg",
            "rate": "0.8%-2%/月",
            "term_range": "6-24",
            "loans_scope": "10000-200000",
            "loan_time": "当天",
            "loans_type": 1,
            "plat_order": 16
        }, {
            "name": "及贷",
            "start": 5,
            "url": "http://m.haomoney.com/activity/reg/register.html?utm_source=wap_daikuanxia",
            "logo": "/upload/image/20170407/1491556034440039101.jpg",
            "rate": "0.03%日",
            "term_range": "1-12",
            "loans_scope": "1000-10000",
            "loan_time": "30分钟",
            "loans_type": 1,
            "plat_order": 17
        }, {
            "name": "曹操贷",
            "start": 5,
            "url": "https://m.ppdai.com/act/cpa/?source=jiekuanwang&url=https://lnk0.com/kYtwl4",
            "logo": "/upload/image/20170216/1487211015074063538.png",
            "rate": "0.03%/日",
            "term_range": "1-3",
            "loans_scope": "500-3000",
            "loan_time": "2小时",
            "loans_type": 1,
            "plat_order": 18
        }, {
            "name": "钱站-快借10万",
            "start": 5,
            "url": "http://app-h5.iqianzhan.com/sources/outsideRegister.html?qd=rxd2",
            "logo": "/upload/image/20161105/1478311975971040435.png",
            "rate": "0.66%-0.95%/月",
            "term_range": "3-36",
            "loans_scope": "5000-100000",
            "loan_time": "当天",
            "loans_type": 1,
            "plat_order": 19
        }, {
            "name": "贷上钱",
            "start": 5,
            "url": "https://www.daishangqian.com/vue/?channel=58788282b8e8#!/active/promotion",
            "logo": "/upload/image/20170113/1484296147365039383.jpg",
            "rate": "0.03%/天",
            "term_range": "1-3",
            "loans_scope": "500-2000",
            "loan_time": "18秒",
            "loans_type": 1,
            "plat_order": 20
        }
    ];
    /*------------------------贷款头部数据-------------------------*/
    var headerData = {
        "loans_scope": [{
            "loans_scope_num": '1',
            "loans_scope": '金额不限'
        }, {
            "loans_scope_num": '2',
            'loans_scope': '0-2000'
        }, {
            "loans_scope_num": '3',
            'loans_scope': '2000-5000'
        }, {
            "loans_scope_num": '4',
            'loans_scope': '5000-10000'
        }, {
            "loans_scope_num": '5',
            'loans_scope': '10000以上'
        }],
        "loans_type": [{
            'loans_type_num': '2',
            "loans_type": "产品类型"
        }, {
            'loans_type_num': '1',
            "loans_type": "信用卡贷"
        }],
        "term_range": [{
            'term_range_num': '1',
            "term_range": "期限不限"
        }, {
            'term_range_num': '2',
            "term_range": "1-6"
        }, {
            'term_range_num': '3',
            "term_range": "7-12"
        }, {
            'term_range_num': '4',
            "term_range": "13-18"
        }, {
            'term_range_num': '5',
            "term_range": "19-24"
        }, {
            'term_range_num': '6',
            "term_range": "25-30"
        }, {
            'term_range_num': '7',
            "term_range": "31-36"
        }]
    };
    var html = "";

    /*function for_data(data){
        for(var i in data){
            var de=data[i];
            for(var j in de){
                if(de[j]==""){
                    console.log(i+"："+j);
                }
            }
        }
    }
    for_data(data);*/
    /*--------------------贷款头部数据加载-------------------------*/
    function header_menu() {
        var header = headerData;
        var loans_scope = "";
        var loans_type = "";
        var term_range = "";
        for (var i in header.loans_scope) {
            var loans_scopes = header.loans_scope[i];
            loans_scope += '<span class="head_block" about="' + loans_scopes.loans_scope_num + '">' + loans_scopes.loans_scope + '</span>';
        }
        for (var k in header.loans_type) {
            var loans_types = header.loans_type[k];
            loans_type += '<span class="head_block" about="' + loans_types.loans_type_num + '">' + loans_types.loans_type + '</span>';
        }
        for (var j in header.term_range) {
            var term_ranges = header.term_range[j];
            term_range += '<span class="head_block" about="' + term_ranges.term_range_num + '">' + term_ranges.term_range + '</span>';
        }
        $('.which_money').html(loans_scope);
        $('.which_kind').html(loans_type);
        $('.which_order').html(term_range);
    }

    header_menu();
    /*-------------------数据绑定----------------------------------*/

    function countList(daikuan) {
        html = "";
        var strstart1 = '';
        var strstart2 = '';
        for (var a = 0; a < daikuan.start; a++) {
            strstart1 += '<img src="img/img-star@3x.png"/>'
        }
        for (var b = 0; b < (5 - daikuan.start); b++) {
            strstart2 += '<img src="img/img-star-half@3x.png"/>'
        }

        html += '<li class="dkx_etail_item">' +
            '<a class="disFlex" href="' + daikuan.url + '">' +
            '<img src="' + http + daikuan.logo + '"/>' +
            '<div class="dkx_etail_item_right">' +
            '<div>' +
            '<div id="productName">' + daikuan.name + '</div>' +
            '<div id="productStart">' + strstart1 + strstart2 + '</div>' +
            '</div>' +
            '<div class="product_item_count">' +
            '<div class="download_num">' +
            '<span>借款范围</span>' +
            '<span>&yen;' + daikuan.loans_scope + '</span>' +
            '</div>' +
            '<div class=" rate_of_interest">' +
            '<span >月利率</span>' +
            '<span class="rate_of_interest_detail">' + daikuan.rate + '</span>' +
            '</div>' +
            '<div class="dkx_date">' +
            '<span>放款时间</span>' +
            '<span>' + daikuan.loan_time + '</span>' +
            '</div>' +
            '</div>' +
            '</div>' +
            '</a>' +
            '</li>';

       $('#dkxDetailItems').append(html);
        var ht= $('#dkxDetailItems').html();
        if(ht==""){
            $('.null_item').css("display","block");
        }else{
            $('.null_item').css("display","none");
        }
    }

    /*--------------------当贷款类型为空------------------------*/
    function infoReuse() {
        //条件查询中贷款期限条件
        var d_ordera = parseInt($("#which_order").attr("about"));
        //条件查询中的金额
        var d_money = parseInt($("#which_money").attr("about"));
        if (d_ordera == 1) {    //贷款期限无
            if (d_money == 1) { //金额无
                for (var q in data) {
                    var d_order = data[q];
                    countList(d_order);
                }
            } else {
                //h获取条件中金额的区域值
                var which_money = $('#which_money').html().split("-");
                for (var q in data) {
                    var d_order = data[q];

                    //获取数据中金额的值
                    var money = d_order.loans_scope.split("-");
                    var money_num = parseInt(money[0]);
                    var money_num1 = parseInt(money[1]);

                    if (which_money.length == 1) {
                        if (money_num1 > 10000) {
                            countList(d_order);
                        } else {
                            continue;
                        }
                    } else {
                        var moneyForif = parseInt(which_money[0]);
                        var moneyForif1 = parseInt(which_money[1]);
                        if ((money_num <= moneyForif && moneyForif <= money_num1) || (moneyForif <= money_num && money_num1 <= moneyForif1) || (money_num <= moneyForif1 && moneyForif1 <= money_num1)) {
                            countList(d_order);
                        } else {
                            continue;
                        }
                    }
                }
            }
        } else {
            //获取条件中贷款期限
            var d_ordera_data_if = $("#which_order").html().split("-");
            var d_data = parseInt(d_ordera_data_if[0]);
            var d_data1 = parseInt(d_ordera_data_if[1]);
            for (var a in data) {
                var d_order1 = data[a];
                //获数据中贷款期限

                var d_ordera_data = d_order1.term_range.split("-");
                var d_item_ordera = parseInt(d_ordera_data[0]);
                var d_item_ordera1 = parseInt(d_ordera_data[1]);
                    if ((d_item_ordera <=d_data  && d_data <= d_item_ordera1) || (d_data <= d_item_ordera && d_item_ordera1 <= d_data1) || (d_item_ordera <=d_data1  && d_data1 <=d_item_ordera1 )) {
                    if (d_money == 1) { //金额无
                        countList(d_order1);
                    } else {
                        //h获取条件中金额的区域值
                        var which_money = $('#which_money').html().split("-");
                        //获取数据中金额的值
                        var money = d_order1.loans_scope.split("-");
                        var money_num = parseInt(money[0]);
                        var money_num1 = parseInt(money[1]);

                        if (which_money.length == 1) {
                            if (money_num1 > 10000) {
                                countList(d_order1);
                            }
                        } else {
                            var moneyForif = parseInt(which_money[0]);
                            var moneyForif1 = parseInt(which_money[1]);
                            if ((money_num <= moneyForif && moneyForif <= money_num1) || (moneyForif <= money_num && money_num1 <= moneyForif1) || (money_num <= moneyForif1 && moneyForif1 <= money_num1)) {
                                countList(d_order1);
                                console.log(d_order1.term_range);
                            } else {
                                continue;
                            }
                        }
                    }
                } else {
                    continue;
                }
            }
        }
    }

    /*--------------------当贷款类型不为为空------------------------*/
    function infoReuseNotNull() {
        var which_kind = parseInt($("#which_kind").attr("about"));
        for (var c in data) {
            var item_data = data[c];
            if (item_data.loans_type == which_kind) {
                //条件查询中贷款期限条件
                var d_ordera = parseInt($("#which_order").attr("about"));
                //条件查询中的金额
                var d_money = parseInt($("#which_money").attr("about"));

                //条件查询中贷款无期限条件
                if (d_ordera == 1) {
                    if (d_money == 1) {
                        countList(item_data);
                    } else {
                        var which_money = $('#which_money').html().split("-");
                        //获取数据源中的金额
                        var money = item_data.loans_scope.split("-");
                        var money_num = parseInt(money[0]);
                        var money_num1 = parseInt(money[1]);

                        if (which_money.length == 1) {
                            if (money_num1 > 10000) {
                                countList(item_data);
                            }
                        } else {
                            var moneyForif = parseInt(which_money[0]);
                            var moneyForif1 = parseInt(which_money[1]);
                            if ((money_num <= moneyForif && moneyForif <= money_num1) || (moneyForif <= money_num && money_num1 <= moneyForif1) || (money_num <= moneyForif1 && moneyForif1 <= money_num1)) {
                                countList(item_data);
                            } else {
                                continue;
                            }
                        }
                    }

                    //条件查询中贷款有期限条件
                } else {
                    //获取条件中贷款期限
                    var d_ordera_data_if = $("#which_order").html().split("-");
                    var d_data = parseInt(d_ordera_data_if[0]);
                    var d_data1 = parseInt(d_ordera_data_if[1]);
                    //获数据中贷款期限
                    var d_ordera_data = item_data.term_range.split("-");
                    var d_item_ordera = parseInt(d_ordera_data[0]);
                    var d_item_ordera1 = parseInt(d_ordera_data[1]);
                    if ((d_item_ordera <=d_data  && d_data <= d_item_ordera1) || (d_data <= d_item_ordera && d_item_ordera1 <= d_data1) || (d_item_ordera <=d_data1  && d_data1 <=d_item_ordera1 )) {
                        if (d_money == 1) { //金额无
                            countList(item_data);
                        } else {
                            //h获取条件中金额的区域值
                            var which_money = $('#which_money').html().split("-");
                            //获取数据中金额的值
                            var money = item_data.loans_scope.split("-");
                            var money_num = parseInt(money[0]);
                            var money_num1 = parseInt(money[1]);

                            if (which_money.length == 1) {
                                if (money_num1 > 10000) {
                                    countList(item_data);
                                }
                            } else {
                                var moneyForif = parseInt(which_money[0]);
                                var moneyForif1 = parseInt(which_money[1]);
                                if ((money_num <= moneyForif && moneyForif <= money_num1) || (moneyForif <= money_num && money_num1 <= moneyForif1) || (money_num <= moneyForif1 && moneyForif1 <= money_num1)) {
                                    countList(item_data);
                                } else {
                                    continue;
                                }
                            }
                        }
                    } else {
                        continue;
                    }

                }
            } else {
                continue;
            }
        }

    }

    /*----------------------条件查询按钮操作------------------------*/
    function onloadListInfo() {
        html = "";
        //条件查询中贷款类型条件
        var which_kind_about = parseInt($('#which_kind').attr("about"));
        $('#dkxDetailItems').html("");
        if (which_kind_about == 2) {
            infoReuse();
        } else {
            infoReuseNotNull();
        }

    }

    /*-------------------点击头部菜单显示相应的内容----------------*/
    $('.head_block-items').on("click", function () {
        var id = $(this).children(".head_title").attr("id");
        var cl = "." + id;
        $(cl).css("display", "block");
        $("#titeBg").css("display","block");
    });
    /*------------------------点击详细内容操作---------------------*/
    $('.head_block').on('click', function () {
        var names = $(this).parents('.dkx_nav').attr("id");
        var val = $(this).html();
        var about = $(this).attr("about");
        var ids = "#which_" + names;
        $(ids).html(val);
        $(ids).attr("about", about);
        $(this).parents(".dkx_nav").css("display", "none");
        $(this).addClass("dkx_nav_active").siblings().removeClass("dkx_nav_active");
        $("#titeBg").css("display","none");
        onloadListInfo();
    });
    $("#titeBg").on("click",function(){
        $('.dkx_nav').css("display", "none");
        $(this).css("display", "none");
    });

    function GetQueryString(key) {
        // key为url变量名称
        var urlGets = location.search.substr(1);
        if (urlGets.length > 0) {
            start = urlGets.indexOf(key + '=');
            if (start != -1) {
                start = start + key.length + 1;
                end = urlGets.indexOf('&', start);
                if (end == -1) {
                    end = urlGets.length
                }
                return decodeURIComponent(urlGets.substring(start, end));
            }
        }
        return '';
    }

    /*--------------------------参数名称---------------------------*/
    /*
     *loans_scope贷款金额范围
     *loans_type贷款类型
     *term_range贷款期限
     * */

    /*--------------------对json数据分析加载---------------------*/
    function queryInformationForData(loans_scope, loans_type, term_range) {
        var loans_scope_detail = parseInt(loans_scope);
        var loans_types = parseInt(loans_type);
        var term_ranges = parseInt(term_range);
        var daikuan_detail = data;
        html = "";
        $("#dkxDetailItems").html("");
        for (var p in daikuan_detail) {
            var dk_item = daikuan_detail[p];
            if (!loans_types || loans_types == dk_item.loans_type) {
                var dk_tang = dk_item.term_range.split("-");
                dk_tang_num = parseInt(dk_tang[0]);
                dk_tang_num1 = parseInt(dk_tang[1]);
                if (!term_ranges || (dk_tang_num <= term_ranges && term_ranges <= dk_tang_num1)) {
                    var dk_scope = dk_item.loans_scope.split("-");
                    dk_scope_num = parseInt(dk_scope[0]);
                    dk_scope_num1 = parseInt(dk_scope[1]);
                    if (!loans_scope_detail || (dk_scope_num <= loans_scope_detail && loans_scope_detail <= dk_scope_num1)) {
                        countList(dk_item);
                    }
                }
            }
        }
    }

    /*--------------------加载时获取数据-------------------------*/
    function loadingQuery() {
        var loans_scope = GetQueryString(loans_scope);
        var loans_type = GetQueryString(loans_type);
        var term_range = GetQueryString(term_range);
        loans_scope = loans_scope || "";
        loans_type = loans_type || "";
        term_range = term_range || "";
        queryInformationForData(loans_scope, loans_type, term_range);
    }

    /*------------------条件选择方法------------------------------*/
    function conditionClick() {
        loadingQuery();
    }

    conditionClick();
};
